// 场景验证脚本 - 检查所有leads_to引用是否都有对应的场景定义

const fs = require('fs');
const path = require('path');

console.log('🔍 开始验证所有场景引用...\n');

// 读取extendedStory.js文件
const extendedStoryPath = path.join(__dirname, 'js', 'extendedStory.js');
const extendedStoryContent = fs.readFileSync(extendedStoryPath, 'utf8');

// 读取gameEngine.js文件
const gameEnginePath = path.join(__dirname, 'js', 'gameEngine.js');
const gameEngineContent = fs.readFileSync(gameEnginePath, 'utf8');

// 读取storyManager.js文件
const storyManagerPath = path.join(__dirname, 'js', 'storyManager.js');
const storyManagerContent = fs.readFileSync(storyManagerPath, 'utf8');

// 合并所有内容
const allContent = extendedStoryContent + '\n' + gameEngineContent + '\n' + storyManagerContent;

// 提取所有leads_to引用
const leadsToRegex = /leads_to:\s*["']([^"']+)["']/g;
const leadsToRefs = [];
let match;
while ((match = leadsToRegex.exec(allContent)) !== null) {
    leadsToRefs.push(match[1]);
}

// 提取所有场景定义
const sceneIdRegex = /id:\s*["']([^"']+)["']/g;
const definedScenes = [];
while ((match = sceneIdRegex.exec(allContent)) !== null) {
    if (match[1].startsWith('chapter')) {
        definedScenes.push(match[1]);
    }
}

// 去重并排序
const uniqueLeadsToRefs = [...new Set(leadsToRefs)].sort();
const uniqueDefinedScenes = [...new Set(definedScenes)].sort();

console.log('📊 统计结果:');
console.log(`   - 找到 ${uniqueLeadsToRefs.length} 个不同的场景引用`);
console.log(`   - 找到 ${uniqueDefinedScenes.length} 个已定义的场景\n`);

// 检查缺失的场景
const missingScenes = uniqueLeadsToRefs.filter(ref => !uniqueDefinedScenes.includes(ref));
const unusedScenes = uniqueDefinedScenes.filter(scene => !uniqueLeadsToRefs.includes(scene));

console.log('❌ 缺失的场景定义:');
if (missingScenes.length === 0) {
    console.log('   ✅ 所有场景引用都有对应的定义！');
} else {
    missingScenes.forEach(scene => {
        console.log(`   - ${scene}`);
    });
}

console.log('\n⚠️  未被引用的场景:');
if (unusedScenes.length === 0) {
    console.log('   ✅ 所有场景定义都被正确引用！');
} else {
    unusedScenes.forEach(scene => {
        console.log(`   - ${scene}`);
    });
}

console.log('\n📋 所有场景引用列表:');
uniqueLeadsToRefs.forEach(ref => {
    const isValid = uniqueDefinedScenes.includes(ref);
    console.log(`   ${isValid ? '✅' : '❌'} ${ref}`);
});

console.log('\n🎯 验证完成！');